package com.hu.zhcc.shiro.controller;

import com.hu.zhcc.shiro.dao.TJ_TMemberMapper;
import com.hu.zhcc.shiro.dao.Tj_TCheckitemMapper;
import com.hu.zhcc.shiro.dao.Tj_TSetmealMapper;
import com.hu.zhcc.shiro.entity.tj.*;
import com.hu.zhcc.shiro.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/tjtmeber")
public class TjTmeberController {
    @Autowired
    private TJ_TMemberService tjTMemberService;//会员表service层
    @Autowired
    private TJ_THealthmgrService tj_tHealthmgrService;//健康管理师
    @Autowired
    private Tj_TMemberDynamicinfoService tjTMemberDynamicinfoService;//会员动态表
    @Autowired
    private Tj_TMemberInfoService tj_tMemberInfoService;//会员详情表
    @Autowired
    private Tj_TMemberMedicalhistoryService tj_tMemberMedicalhistoryService;//会员病史表
    @Autowired
    private Tj_TCheckitemService tj_tCheckitemService;//检查项表
    @Autowired
    private TJ_TSetmealService tj_tSetmealService;//套餐表
    @Autowired
    private Tj_VipsettingService vipsettingService;
    @Autowired
    private Tj_TInterposeFollowupVisitService tj_tInterposeFollowupVisitService;//随访日记


    //查询所有
    @GetMapping("/all")
    public List<TMember> queryAll(){
        return tjTMemberService.queryAll();
    }

    //查询所有健康师表
    @GetMapping("/allThealthmgr")
    public List<THealthmgr> queryAllTh(){
     return    tj_tHealthmgrService.queryAll();
    }

    //模糊查询会员
    @GetMapping("")
    public List<TMember> listTmember(TMember tMember){
        return tjTMemberService.queryLike(tMember);
    }

    //删除会员表 会员动态 会员详情 会员病史的编号
    @DeleteMapping("/{id}")
    public int delTmember(@PathVariable("id")Integer id){
      tjTMemberService.delTmember(id);
      tjTMemberDynamicinfoService.deleteByPrimaryKey(id);
      tj_tMemberMedicalhistoryService.deleteByPrimaryKey(id);
        int i = tj_tMemberInfoService.deleteByPrimaryKey(id);
        return i;
    }

    //根据编号查询会员表 会员动态 会员详情 会员病史
    @GetMapping("/{id}")
    public VipdaTj queryById(@PathVariable("id") Integer id){
        TMember tMember = tjTMemberService.queryById(id);
        TMemberDynamicinfo tMemberDynamicinfo = tjTMemberDynamicinfoService.queryByIdDynamicinfo(id);
        TMemberInfo tMemberInfo = tj_tMemberInfoService.queryByIdTmemberinfo(id);
        TMemberMedicalhistory tMemberMedicalhistory = tj_tMemberMedicalhistoryService.queryByIdhistory(id);
        VipdaTj vipdaTj=new VipdaTj();
        vipdaTj.setMember(tMember);
        vipdaTj.setTmemberdynamicinfo(tMemberDynamicinfo);
        vipdaTj.setTmemberinfo(tMemberInfo);
        vipdaTj.setTmembermedicalhistory(tMemberMedicalhistory);
        return vipdaTj;
    }

    //修改会员表 会员动态 会员详情 会员病史
    @PutMapping("")
    public int updTmember(@RequestBody VipdaTj vipdaTj){
        System.out.println(vipdaTj);
        //会员表
        tjTMemberService.updTmember(vipdaTj.getMember());
        //会员动态表
        tjTMemberDynamicinfoService.updDynamicinfo(vipdaTj.getTmemberdynamicinfo());
        //会员详情表
        tj_tMemberInfoService.updInfo(vipdaTj.getTmemberinfo());
        //会员病史表
        int i = tj_tMemberMedicalhistoryService.updMedicalhistory(vipdaTj.getTmembermedicalhistory());
        return i;
    }

    //添加会员表 会员动态 会员详情 会员病史
    @PostMapping("")
    public int addTmember(@RequestBody VipdaTj vipdaTj){
         tjTMemberService.addTmember(vipdaTj.getMember());
            Calendar c=Calendar.getInstance();//获取当前年月日
            int n = c.get(Calendar.YEAR);
            int y = c.get(Calendar.MONTH)+1;
            int r = c.get(Calendar.DAY_OF_MONTH);
            String rq=n+"-"+y+"-"+r;
            Vipsetting vipsetting = vipsettingService.queryByrq(rq);//查询vip设置表里面当天有没有人新增一个

            System.out.println(vipsetting);
            int i =0;
            if(vipsetting==null){
                i=vipsettingService.add(rq);//没有就加一条数据
            }
            if(vipsetting!=null){
                i=vipsettingService.add(rq);//有就加个1
            }
         tj_tMemberInfoService.insertinfo(vipdaTj.getTmemberinfo());
         tj_tMemberMedicalhistoryService.inserthistory(vipdaTj.getTmembermedicalhistory());
         tjTMemberDynamicinfoService.insertDynamicinfo(vipdaTj.getTmemberdynamicinfo());
         tj_tInterposeFollowupVisitService.inserts(vipdaTj.gettInterposeFollowupVisit());
        return i;
    }





}
